<html>
<head><meta charset="utf-8"><title>Transmuting MaybeUninit · project-safe-transmute · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/index.html">project-safe-transmute</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html">Transmuting MaybeUninit</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208718290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208718290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208718290">(Sep 01 2020 at 17:10)</a>:</h4>
<p>Are there any plans for supporting transmutes of <code>MaybeUninit&lt;T&gt;</code> to <code>MaybeUninit&lt;U&gt;</code>?</p>



<a name="208718347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208718347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208718347">(Sep 01 2020 at 17:11)</a>:</h4>
<p>In my code I have a <code>Pod</code> trait with these methods:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">as_uninit_bytes_mut</span><span class="p">(</span><span class="n">s</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">];</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">slice_as_uninit_bytes_mut</span><span class="p">(</span><span class="n">s</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">];</span><span class="w"></span>
</code></pre></div>



<a name="208718681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208718681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208718681">(Sep 01 2020 at 17:13)</a>:</h4>
<p>I've found these extremely useful when reading data from a file:</p>
<ul>
<li>Start with a <code>MaybeUninit&lt;T&gt;</code></li>
<li>Turn that into a <code>&amp;mut [MaybeUninit&lt;u8&gt;]</code> (which can be passed to <code>ReadBuf</code>, see RFC 2930)</li>
<li>Get a <code>&amp;mut [u8]</code> back from <code>read_buffer</code> which contains the initialized part of the buffer (the bytes that were read).</li>
<li>Turn that into a <code>T</code>.</li>
</ul>



<a name="208718814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208718814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208718814">(Sep 01 2020 at 17:14)</a>:</h4>
<p>This can all be done with purely safe code (with the <code>Pod</code> trait handling the unsafe transmutes).</p>



<a name="208718854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208718854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208718854">(Sep 01 2020 at 17:14)</a>:</h4>
<p>It would be nice if something like this was covered by the RFC.</p>



<a name="208743486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Transmuting%20MaybeUninit/near/208743486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Transmuting.20MaybeUninit.html#208743486">(Sep 01 2020 at 20:26)</a>:</h4>
<p>I see no reason why this transmutation wouldn't be supported out-of-the-box. The RFC, as described, will handle that use-case.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>